<?php

class BildUploadController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        
    }

    public function addAction()
    {

        $this->view->bodyCopy = "<p>Foto hochladen.</p>";
        $this->view->anzeige = false;
        
        $form = new Application_Form_BildUpload();
        $this->view->form = $form;
        $request = $this->getRequest();
        $form->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
        
        if ($request->isPost())
        {
        	$formData = $request->getPost();
        	if ($form->isValid($formData))
        	{
        		$auth = Zend_Auth::getInstance();
        		if ($auth->hasIdentity()) {
        			$id = $auth->getIdentity()->id;
        				
        			$mapper = new Application_Model_Mapper_BildMapper();
            		$upload = new Zend_File_Transfer_Adapter_Http();
             		$endung = substr($upload->getFileName(), strrpos($upload->getFileName(), "."), strlen($upload->getFileName()));
            	 	$generatedid = Helper_Idgenerator::getid();
             		$upload->addFilter('Rename', '/var/www/sailorbase-a/sailorbase-a/public/userbilder/' . $generatedid . $endung);
			
            		try {
            			$upload->receive();
             			$test2 = $upload->getFileInfo();
            			$this->view->anzeige = true;
            			$this->view->link = $generatedid;
            			
            			$bild = new Application_Model_BildUpload($form->getValues());
            			$bild->setId($generatedid);
            			$bild->setReferenzId($id);
            			$bild->setReferenzObjekt('user');
            			$mapper->add($bild);

            		}
            		catch (Zend_File_Transfer_Exception $e) {
            		$e->getMessage();
            		}
        		
        		} 
        		else
        		{
              	$form->populate($formData);
				}            
      	 	 }
        }
        $this->view->form = $form;
    }

    public function deleteAction()
    {
    	$referenzid = $this->_getParam('referenzId');
    	$id = $this->_getParam('id');
    	$bildid = $this->_getParam('bildId');
    	$viewbarid = $this->_getParam('viewbarid');
    	$auth = Zend_Auth::getInstance();
    	if ($auth->hasIdentity()) {
    		$iduser = $auth->getIdentity()->id;
    	
    		$bildmapper = new Application_Model_Mapper_BildMapper();
//     		$bild = $bildmapper->fetchID($referenzid);
   			$bildmapper->delete($bildid);
   			$bildmapper->delete($referenzid);
   			$bildmapper->delete($id);
					
// 					$this->_redirector->gotoSimple('showgroups',
// 							'Group',
// 							null,
// 							array('viewbarid' => $viewbarid)
// 					);
    		}  	    	
    }

    public function showbilderAction()
    {        	
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$id = $auth->getIdentity()->id;

			$bildmapper = new Application_Model_Mapper_BildMapper();
			$anzeige = $bildmapper->fetchAll($id);

			$this->view->entry = $anzeige;
		}
		else{
			echo "Du musst eingeloggt sein.";
		}
    }

}
